import typeToReducer from 'type-to-reducer'
import {
  GET_GOODS_LIST,
  GET_GOODS_INFO,
  GET_GOODS_KEYWORD,
  GET_GOODS_KEY,
  GET_GOODS_CATE,
  GET_GOODS_CONTENT,
  GET_GOODS_CATE_TOP,
  GET_GOODS_LIST_INDEX
} from './../constants/actionTypes'

const initialState = {
  data: [],
  isLoading: false,
  isRefreshing: false,
  levelRatio: 0,
  return_one_price: 0,
  page: 1,
  error: false,
}

const initialState2 = {
  data: null,
  isLoading: false,
  error: false,
}

const initialState3 = {
  data: [],
  isLoading: false,
  error: false,
}


export const goodsListReducer = typeToReducer({
  [GET_GOODS_LIST]: {
    LOADING: (state, action) => {
      let isRefreshing = false;
      if (action.meta.page == 1) {
        isRefreshing = true;
      } else {
        isRefreshing = false;
      }
      return {
        ...initialState,
        isLoading: true,
        page: action.meta.page,
        isRefreshing
      }
    },
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data.data,
        page: action.meta.page,
        levelRatio: action.payload.data.levelRatio,
        return_one_price: action.payload.data.returnOnePirce
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload,
      page: action.meta.page - 1,
    })
  }
}, initialState)


export const goodsListIndexReducer = typeToReducer({
  [GET_GOODS_LIST_INDEX]: {
    LOADING: (state, action) => {
      let isRefreshing = false;
      if (action.meta.page == 1) {
        isRefreshing = true;
      } else {
        isRefreshing = false;
      }
      return {
        ...initialState,
        isLoading: true,
        page: action.meta.page,
        isRefreshing
      }
    },
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data.data,
        page: action.meta.page,
        levelRatio: action.payload.data.levelRatio,
        return_one_price: action.payload.data.returnOnePirce
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload,
      page: action.meta.page - 1,
    })
  }
}, initialState)


export const goodsInfoReducer = typeToReducer({
  [GET_GOODS_INFO]: {
    LOADING: (state, action) => {
      return {
        ...initialState2,
        isLoading: true,
      }
    },
    SUCCESS: (state, action) => {
      return {
        ...initialState2,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState2,
      error: action.payload,
    })
  }
}, initialState2)


export const goodsContentReducer = typeToReducer({
  [GET_GOODS_CONTENT]: {
    LOADING: (state, action) => {
      return {
        ...initialState2,
        isLoading: true,
      }
    },
    SUCCESS: (state, action) => {
      return {
        ...initialState2,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState2,
      error: action.payload,
    })
  }
}, initialState2)


export const goodsKeywordReducer = typeToReducer({
  [GET_GOODS_KEYWORD]: {
    LOADING: (state, action) => {
      return {
        ...initialState3,
        isLoading: true,
      }
    },
    SUCCESS: (state, action) => {
      return {
        ...initialState3,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState3,
      error: action.payload,
    })
  }
}, initialState3)


export const goodsKeyReducer = typeToReducer({
  [GET_GOODS_KEY]: {
    LOADING: (state, action) => {
      let isRefreshing = false;
      if (action.meta.page == 1) {
        isRefreshing = true;
      } else {
        isRefreshing = false;
      }
      return {
        ...initialState,
        isLoading: true,
        page: action.meta.page,
        isRefreshing
      }
    },
    SUCCESS: (state, action) => {
      return {
        ...initialState,
        data: action.payload.data.data,
        page: action.payload.data.page,
        levelRatio: action.payload.data.levelRatio,
        return_one_price: action.payload.data.returnOnePirce
      }
    },
    ERROR: (state, action) => ({
      ...initialState,
      error: action.payload,
      page: action.meta.page - 1,
    })
  }
}, initialState)


export const goodsCateReducer = typeToReducer({
    [GET_GOODS_CATE]: {
        LOADING: (state, action) => {
            return {
                ...initialState3,
                isLoading: true,
            }
        },
        SUCCESS: (state, action) => {
            return {
                ...initialState3,
                data: action.payload.data,
            }
        },
        ERROR: (state, action) => ({
            ...initialState3,
            error: action.payload,
        })
    }
}, initialState3)


export const goodsCateTopReducer = typeToReducer({
  [GET_GOODS_CATE_TOP]: {
    LOADING: (state, action) => {
      return {
        ...initialState3,
        isLoading: true,
      }
    },
    SUCCESS: (state, action) => {
      return {
        ...initialState3,
        data: action.payload.data,
      }
    },
    ERROR: (state, action) => ({
      ...initialState3,
      error: action.payload,
    })
  }
}, initialState3)